package com.hexacta.hrs.sandbox.tasks2.actions;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.hexacta.hrs.sandbox.tasks2.model.Activity;
import com.hexacta.hrs.sandbox.tasks2.model.Category;
import com.hexacta.hrs.sandbox.tasks2.model.Task;
import com.hexacta.hrs.sandbox.tasks2.model.User;
import com.hexacta.hrs.sandbox.tasks2.service.CategoryService;
import com.hexacta.hrs.sandbox.tasks2.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

@ContextConfiguration(locations = "/spring-orm-config.xml")
public class TaskAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2489941976537757304L;

	@Autowired
	private UserService userService;

	private String id;
	private User user;
	private String newTaskName;
	private String newTaskDuration;
	private String selectedCategory;
	private Category category;
	private String selectedActivity;
	private Activity activity;

	@Autowired
	private CategoryService categoryService;

	private String modifiedTaskDuration;
	private String selectedTask;
	private Task task;

	public String newTask() {

		this.setUser(this.getUserService().findById(new Long(this.getId())));

		this.setActivity(this.getUserService().findActivityById(
				new Long(this.getSelectedActivity())));

		this.setCategory(this.getCategoryService().findCategoryById(
				new Long(this.getSelectedCategory())));

		this.getUserService().addTask(
				this.getActivity(),
				new Task(this.getNewTaskName(), this.getCategory(), Integer
						.parseInt(this.getNewTaskDuration())));

		return SUCCESS;
	}

	public String modifyTaskDuration() {

		this.setActivity(this.getUserService().findActivityById(
				new Long(this.getId())));

		this.setTask(this.getUserService().findTaskById(
				new Long(this.getSelectedTask())));

		this.getUserService().modifyTaskDuration(this.getTask(),
				Integer.parseInt(this.getModifiedTaskDuration()));

		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getNewTaskName() {
		return newTaskName;
	}

	public void setNewTaskName(String newTaskName) {
		this.newTaskName = newTaskName;
	}

	public String getNewTaskDuration() {
		return newTaskDuration;
	}

	public void setNewTaskDuration(String newTaskDuration) {
		this.newTaskDuration = newTaskDuration;
	}

	public String getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getSelectedActivity() {
		return selectedActivity;
	}

	public void setSelectedActivity(String selectedActivity) {
		this.selectedActivity = selectedActivity;
	}

	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public List<Category> getAllCategories() {

		return this.getCategoryService().selectAllCategories();
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setModifiedTaskDuration(String modifiedTaskDuration) {
		this.modifiedTaskDuration = modifiedTaskDuration;
	}

	public String getModifiedTaskDuration() {
		return modifiedTaskDuration;
	}

	public void setSelectedTask(String selectedTask) {
		this.selectedTask = selectedTask;
	}

	public String getSelectedTask() {
		return selectedTask;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Task getTask() {
		return task;
	}

}
